<template>
  <basic-container>
    <avue-crud
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      :before-open="beforeOpen"
      @on-load="getList"
      @search-change="searchChange"
      @search-reset="searchReset"
      @row-update="handleUpdate"
      @row-save="handleSave"
      @row-del="handleDelete"
    >
      <template v-slot:objName="{row}">
        <el-button
          type="text"
          @click="showPetDetail(row.objId)">{{ row.objName }}</el-button>
      </template>
    </avue-crud>
    <detailPopup
      v-if="petId"
      :id="petId"
      v-model="petDetailVisible"
    ></detailPopup>
  </basic-container>
</template>

<script>
import {
  addObj,
  delObj,
  fetchList,
  putObj,
  getObj,
} from '@/api/core/recommendMng';
import { tableOption } from '@/const/crud/pet/recommendMng';
import detailPopup from '../base-file/components/detailPopup.vue';
import { mapGetters } from 'vuex';
import crudMixins from '@/mixins/crud';
export default {
  name: 'customerManage',
  components: { detailPopup },
  mixins: [crudMixins],
  data() {
    return {
      editVisible: false,
      form: {},
      tableOption: tableOption(),
      userId: null,
      petId: null,
      petDetailVisible: false,
      addVisible: false,
    };
  },
  computed: {
    ...mapGetters(['permissions']),
  },
  watch: {},
  methods: {
    edit(row) {
      this.editVisible = true;
      this.userId = row.userId;
    },
    add() {
      this.addVisible = true;
    },
    // 列表查询
    getList(page, refresh) {
      if (refresh) {
        this.page.current = 1;
        this.page.currentPage = 1;
      }
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize,
          },
          this.searchForm
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    // 打开前的拦截
    async beforeOpen(done, type) {
      if (type !== 'add') {
        const res = await getObj(this.form.id);
        this.form = { ...res.data.data };
      }
      done();
    },
    // 保存
    async handleSave(row, done, loading) {
      const obj = JSON.parse(row.objName);
      try {
        await addObj({ ...row, objName: obj.nickname, objId: obj.id });
        done();
        this.$message.success('新增成功');
        this.getList(this.page, true);
      } catch (error) {
        loading();
        throw new Error(error);
      }
    },
    // 删除
    handleDelete(row, index) {
      this.$confirm('确定取消推荐该内容并删除推荐设置吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        await delObj(row.id);
        this.$message.success('删除成功');
        this.getList();
      });
    },
    // 编辑
    async handleUpdate(row, index, done, loading) {
      let obj = {};
      if (row.objName.startsWith('{"') && row.objName.endsWith('"}')) {
        obj = JSON.parse(row.objName);
      } else {
        obj = { nickname: row.objName, id: row.objId };
      }
      try {
        await putObj({ ...row, objName: obj.nickname, objId: obj.id });
        done();
        this.$message.success('修改成功');
        this.getList();
      } catch (error) {
        loading();
        throw new Error(error);
      }
    },
    showPetDetail(id) {
      this.petId = id;
      this.petDetailVisible = true;
    },
  },
};
</script>
